package com.mazegame.frontend;

import com.mazegame.backend.BackgroundPanel;
import com.mazegame.backend.ImageDisplayPane;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

import javax.swing.*;
import java.awt.*;

public class CharacterPanel extends JPanel implements GamePanel{
    private CardLayout cardLayout;
    private JPanel cardPanel;
    private JLayeredPane layeredPane;
    private static final int WIDTH = 1500;
    private static final int HEIGHT = 900;
    private JFXPanel jfxPanel;


    public CharacterPanel(CardLayout cardLayout, JPanel cardPanel) {
        this.cardLayout = cardLayout;//
        this.cardPanel = cardPanel;
        initializeUI();
    }

    private void initializeUI() {
        // 使用 JLayeredPane 管理不同层级的组件
        layeredPane = new JLayeredPane();
        setLayout(new BorderLayout());
        add(layeredPane, BorderLayout.CENTER);

        // 创建背景面板并添加到最底层
        BackgroundPanel backgroundPanel = new BackgroundPanel("/img/角色信息界面.png");
        backgroundPanel.setBounds(0, 0, WIDTH, HEIGHT);
        layeredPane.add(backgroundPanel, JLayeredPane.DEFAULT_LAYER);

        // 创建 JavaFX 内容面板（登录界面）
        jfxPanel = new JFXPanel();
        jfxPanel.setOpaque(false); // 确保 JFXPanel 透明
        jfxPanel.setBounds(0, 0, WIDTH, HEIGHT);
        layeredPane.add(jfxPanel, JLayeredPane.MODAL_LAYER); // 放在上层

        Platform.runLater(() -> {

            // 创建主容器
            BorderPane root = createCharacterPane();
            //设置root透明
            root.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");
            // 创建透明场景
            Scene scene = new Scene(root, WIDTH, HEIGHT, Color.TRANSPARENT);
            jfxPanel.setScene(scene);
        });
    }

    private BorderPane createCharacterPane(){
        BorderPane root = new BorderPane();

//        // 创建顶部区域并设置背景颜色
//        Region topRegion = createTitleRegion();
//        topRegion.setStyle("-fx-background-color: rgba(0, 255, 0, 1);"); // 绿色透明背景
//
//        // 创建中心区域并设置背景颜色
//        Region centerRegion = createCharacterRegion();
//        centerRegion.setStyle("-fx-background-color: rgba(0, 0, 255, 1);"); // 蓝色透明背景
//
//        // 创建底部区域并设置背景颜色
//        Region bottomRegion = createControlRegion();
//        bottomRegion.setStyle("-fx-background-color: rgba(255, 0, 0, 1);"); // 红色透明背景
//
//        // 设置 BorderPane 的各个区域
//        root.setTop(topRegion);
//        root.setCenter(centerRegion);
//        root.setBottom(bottomRegion);

        root.setTop(createTitleRegion());
        root.setCenter(createCharacterRegion());
        root.setBottom(createControlRegion());

//
//        // 设置中心区域的最小和最大高度
////        root.centerProperty().setMinHeight(600);
////        root.centerProperty().get().setMaxHeight(Region.USE_PREF_SIZE);

        return root;
    }

    private Region createCharacterRegion() {
        HBox characterBox = new HBox(120);
        characterBox.setPadding(new Insets(20));
        characterBox.setAlignment(Pos.CENTER);
       // characterBox.setPrefHeight(500); // 显式设置 HBox 的高度


        CharacterCard linaCard = new CharacterCard(
                "主角莉娜",
                "身份:天文考古学家,拥有祖辈留下的青铜星盘,为探索天枢之眼,来到了沙漠",
                3,
                "/img/主角.jpg"
        );

        CharacterCard starGuardCard = new CharacterCard(
                "星灵守卫",
                "身份:被星辰之力影响而变成怪物的入侵者,徘徊在星象神庙附近",
                1,
                "/img/怪兽.png"
        );

        characterBox.getChildren().addAll(linaCard, starGuardCard);
        return characterBox;
    }

    private class CharacterCard extends VBox {
        private String name;
        private String identity;
        private int health;
        private String imageUrl;

        public CharacterCard(String name, String identity, int health, String imageUrl) {
            this.name = name;
            this.identity = identity;
            this.health = health;
            this.imageUrl = imageUrl;

            setPrefSize(500, 600);
            setSpacing(20);
            setPadding(new Insets(20));
//            setStyle("-fx-background-color: rgba(255, 255, 255, 0.3); " +
//                    "-fx-background-radius: 24; " +
//                    "-fx-border-radius: 24; " +
//                    "-fx-border-width: 1; " +
//                    "-fx-border-color: rgba(1, 1, 1, 0.2); " +
//                    "-fx-effect: dropshadow(gaussian, rgba(0, 0, 0, 0.7), 10, 0, 0, 0.2);");
            setStyle("-fx-background-color:  rgba(99,110,114,0.2); " +
                    "-fx-background-radius: 24; " +
                    "-fx-border-radius: 24; " +
                    "-fx-border-width: 3; " +
                    "-fx-border-color: rgba(108, 92, 231, 0.3); " +
                    "-fx-effect: none;");

            ImageDisplayPane imagePane = new ImageDisplayPane(imageUrl,460,320,20);

            Label nameLabel = new Label(name);
            nameLabel.setFont(Font.font("Arail", FontWeight.BOLD, 24));
            nameLabel.setTextFill(Color.WHITE);

            Label identityLabel = new Label(identity);
            identityLabel.setFont(Font.font("Arail", 24));
            identityLabel.setTextFill(Color.WHITE);
            identityLabel.setWrapText(true);
            identityLabel.setMaxWidth(460);

            Label healthLabel = new Label("生命值: " + health);
            healthLabel.setFont(Font.font("KaiTi", FontWeight.BOLD, 28));
            healthLabel.setTextFill(Color.rgb(255, 234, 0));

            getChildren().addAll(imagePane, nameLabel, identityLabel, healthLabel);

        }
    }

    private Region createControlRegion() {
        HBox controlBox = new HBox(180);
        controlBox.setPadding(new Insets(20));
        controlBox.setAlignment(Pos.TOP_CENTER);
        controlBox.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");
        // 设置controlBox初始高度
        controlBox.setPrefHeight(180);

        Button backButton = createButton("返回");
        backButton.setOnAction(e -> cardLayout.show(cardPanel, "MAINPANEL"));

//        Button startButton = createButton("开始游戏");
//        startButton.setOnAction(e -> {
//            System.out.println("进入游戏");
//            cardLayout.show(cardPanel, "OPTIONPANEL");
//        });

        controlBox.getChildren().addAll(backButton);
        return controlBox;
    }

    private Button createButton(String text) {
        Button button = new Button(text);
        button.setFont(Font.font("KaiTi", javafx.scene.text.FontWeight.BOLD, 40));
        button.setTextFill(Color.WHITE);
        button.setPrefSize(WIDTH/6, 65);
        button.setStyle("-fx-background-color: #6C5CE7; -fx-background-radius: 10;");
        // 添加悬停效果
        button.setOnMouseEntered(e -> {
            button.setStyle("-fx-background-color: #8C7DF7; -fx-background-radius: 10;");
            button.setEffect(new DropShadow(25, Color.rgb(108, 92, 231, 0.5)));
        });

        button.setOnMouseExited(e -> {
            button.setStyle("-fx-background-color: #6C5CE7; -fx-background-radius: 10;");
            button.setEffect(null);
        });

        return button;
    }

    private Region createTitleRegion() {
        HBox titleBox = new HBox();
        titleBox.setPadding(new Insets(25));
        titleBox.setAlignment(Pos.CENTER);
        //titleBox.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");

        javafx.scene.control.Label titleLabel = new Label("角色介绍");
        titleLabel.setFont(Font.font("KaiTi", FontWeight.BOLD, 64));
        titleLabel.setTextFill(Color.WHITE);
        titleLabel.setEffect(new DropShadow(20, Color.BLACK));

        titleBox.getChildren().add(titleLabel);
        titleBox.setPrefHeight(120); // 设置顶部区域的固定高度

        return titleBox;
    }

    @Override
    public void showPanel() {
        cardLayout.show(cardPanel, "CHARACTERPANEL");
    }

    @Override
    public void cleanup() {

    }
}
